package com.situ.mall.manager.mapper;


import com.situ.mall.manager.entity.Goods;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface GoodsMapper {

    //1、增  插入数据
    @Insert({
            "insert into goods",
            "(name,dscp,price,mark_price,color,version,count,content,recom,category_id)",
            "value(#{name},#{dscp},#{price},#{markPrice},#{color},#{version},#{count},#{content},#{recom},#{categoryId})",
    })
    @Options(useGeneratedKeys = true, keyColumn = "id", keyProperty = "id")
    //在插入时获取表的自增主键 插图片的时候必须知道插入的id
    int insert(Goods goods);


    //2、根据id删除
    @Delete("delete from goods where id=#{id}")
    int delete(Integer id);

    //3、根据id更新,使用动态SQL,需要为SQL添加<script></script>
    @Update({"<script>",
            "update goods",
            "<set>",
                "<if test='name != null and name.length>0'> name=#{name},</if>",
                "<if test='dscp != null and dscp.length>0'> dscp=#{dscp},</if>",
                "<if test='price != null'> price=#{price},</if>",
                "<if test='markPrice != null'> mark_price=#{markPrice},</if>",
                "<if test='color != null and color.length>0'> color=#{color},</if>",
                "<if test='version!= null and version.length>0'> version=#{version},</if>",
                "<if test='count != null'> count=#{count},</if>",
                "<if test='content != null and content.length>0'> content=#{content},</if>",
                "<if test='categoryId != null'> category_id=#{categoryId},</if>",
                "<if test='recom != null'> recom=#{recom}</if>",
                "<if test='status != null'> status=#{status}</if>",
            "</set>",
            "where id=#{id}",
            "</script>",
    })
    int update(Goods goods);



    //4、根据id查询
    @Results({
            @Result(column = "id",property = "id",id=true),
            @Result(column = "id" , property = "pics",
            many = @Many(select = "com.situ.mall.manager.GoodsPicMapper.selectByGoodsId"))
    })
    @Select("select * from goods where id=#{id}")
    Goods selectById(Integer id);



    //5、通用多条件组合查询
    @Select({"<script>",
            "select * from goods",
            "<where>",
            "<if test='name != null and name.length>0'> name like '%${name}%' </if>",
            "<if test='categoryId != null'> and category_id=#{categoryId} </if>",
            "<if test='recom != null'> and recom=#{recom} </if>",
            "<if test='status != null'> and status=#{status} </if>",
            "</where>",
            "</script>"
    })
    @Results({//关联图片 分类
            @Result(column = "id",property = "id",id=true),
            @Result(column="category_id",property="category",
                    one = @One(select="com.situ.mall.manager.mapper.CategoryMapper.selectById")),
            @Result(column = "id" , property = "pics",
                    many = @Many(select = "com.situ.mall.manager.mapper.GoodsPicMapper.selectByGoodsId"))
    })
    List<Goods> select(Goods goods);
}
